import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import axios from 'axios'
import { API_URL } from '@/api/env'

// 定义请求参数类型
export interface RequestParams {
    method: 'GET' | 'POST' | 'PUT' | 'DELETE'
    url: string
    data?: any
}

// 定义响应数据类型
export interface ResponseData {
    success: boolean
    data?: any
    message?: string
}

// HttpClient 类
class HttpClient {
    // 发送请求的方法
    async sendRequest(params: RequestParams): Promise<ResponseData> {
        try {
            const config: AxiosRequestConfig = {
                method: params.method,
                url: API_URL + params.url,
                data: params.data
                // 可以在此处添加其他配置，如 headers、timeout 等
            }

            const response: AxiosResponse = await axios(config)
            return { success: true, data: response.data }
        } catch (error) {
            // 处理请求错误
            console.error('Request error:', error)
            return { success: false, message: error.message }
        }
    }
}

// 导出 HttpClient 类的单例实例
export const httpClient = new HttpClient()
